package com.controller.system.sysdictcontroller;

import com.alibaba.fastjson.JSONObject;
import com.entity.SysDict;
import com.service.SysDictService;
import com.utils.BaseController;
import com.utils.DataTables;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 *  数据字典Controller
 */
@Controller
@RequestMapping("/dict")
public class SysDictController extends BaseController{

    @Autowired
    HttpServletRequest request;// 请求

    @Autowired
    SysDictService sysDictService;

    /**
     * 显示所有(废品管理模块)
     *
     * @return
     */
    @RequestMapping(value = "/selectAll.do",method = RequestMethod.POST,produces = "application/json; charset=utf-8")
    @ResponseBody
    public String selectAll(){
        List<SysDict> list=new ArrayList<>();
        for (SysDict sysDict : sysDictService.selectSysDict()) {
            if (sysDict.getDictText()!=null){
                list.add(sysDict);
            }
        }
        return JSONObject.toJSONString(list);
    }

    /**
     * 分页显示列表
     *
     * @return
     */
    @RequestMapping(value = "/getPageList.do",method = RequestMethod.POST,produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getPageList(){
        return JSONObject.toJSONString(sysDictService.getPageList(DataTables.getInstance(request,null)));
    }

    /**
     * 获取详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/getDictById.do",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getDictById(Integer id){
        return JSONObject.toJSONString(sysDictService.selectSysDictById(id));
    }

    /**
     * 更新SysDict
     *
     * @param sysDict
     * @return
     */
    @RequestMapping(value = "/update.do",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String updateSysDict(@ModelAttribute SysDict sysDict){
        return JSONObject.toJSONString(sysDictService.updateSysDict(sysDict));
    }

    /**
     * 添加SysDict
     *
     * @param sysDict
     * @return
     */
    @RequestMapping(value = "/add.do",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String addSysDict(SysDict sysDict){
        return JSONObject.toJSONString(sysDictService.addSysDict(sysDict));
    }

    /**
     * 删除SysDict
     *
     * @param idList    //要删除的id数组
     * @return
     */
    @RequestMapping(value = "/del.do",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public String deleteSysDict(@RequestParam("idlist[]") List<Integer> idList){
        return JSONObject.toJSONString(sysDictService.delScrap(idList));
    }

}
